<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>insertionSort</title>
</head>

<body>
    <div>打开浏览器控制台查看结果</div>
    <script>
        const insertionSort = arr =>
            arr.reduce((res, i) => {
                if (!res.length) {
                    return [i];
                }
                res.some((j, index) => {
                    if (i <= j) {
                        res.splice(index, 0, i);
                        return true;
                    }
                    if (i >= j && index === res.length - 1) {
                        res.splice(index + 1, 0, i);
                        return true;
                    }
                    return false;
                });
                return res;
            }, []);
        console.log(insertionSort([6, 3, 4, 1]));
    </script>
</body>

</html>